<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="客户姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cusName">
              <a-input v-model="model.cusName" placeholder="请输入客户姓名"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="客户电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cusTel">
              <a-input v-model="model.cusTel" placeholder="请输入客户电话"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="电子邮箱" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cusEmail">
              <a-input v-model="model.cusEmail" placeholder="请输入电子邮箱"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="课题组" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cusKtz">
              <a-input v-model="model.cusKtz" placeholder="请输入课题组"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="客户单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cusCompany">
              <a-input v-model="model.cusCompany" placeholder="请输入客户单位"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="客户地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cusAddress">
              <a-input v-model="model.cusAddress" placeholder="请输入客户地址"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="秘钥" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="token">
              <a-input v-model="model.token" placeholder="请输入秘钥"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="头像url" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="avatarUrl">
              <a-input v-model="model.avatarUrl" placeholder="请输入头像url"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="性别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="gender">
              <j-dict-select-tag type="list" v-model="model.gender" dictCode="sex" placeholder="请选择性别" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="国家" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="country">
              <a-input v-model="model.country" placeholder="请输入国家"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="省" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="province">
              <a-input v-model="model.province" placeholder="请输入省"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="城市" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="city">
              <a-input v-model="model.city" placeholder="请输入城市"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="微信openid" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="openId">
              <a-input v-model="model.openId" placeholder="请输入微信openid"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="小程序unionid" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unionId">
              <a-input v-model="model.unionId" placeholder="请输入小程序unionid"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="系统用户id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sysUserId">
              <a-input v-model="model.sysUserId" placeholder="请输入系统用户id"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'BusCustomerForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           cusName: [
              { required: true, message: '请输入客户姓名!'},
           ],
           cusTel: [
              { required: true, message: '请输入客户电话!'},
           ],
           cusEmail: [
              { required: true, message: '请输入电子邮箱!'},
           ],
           cusKtz: [
              { required: true, message: '请输入课题组!'},
           ],
           cusCompany: [
              { required: true, message: '请输入客户单位!'},
           ],
           cusAddress: [
              { required: true, message: '请输入客户地址!'},
           ],
        },
        url: {
          add: "/customer/busCustomer/add",
          edit: "/customer/busCustomer/edit",
          queryById: "/customer/busCustomer/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>